package com.txzt.gaea.provide.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.api.client.AdminUserRemoteService;
import com.txzt.gaea.dto.MenuQueryRemoteDTO;
import com.txzt.gaea.dto.UserPageAllDTO;
import com.txzt.gaea.dto.UserPageDTO;
import com.txzt.gaea.dto.UserTokenDTO;
import com.txzt.gaea.vo.MenuSSVO;
import com.txzt.gaea.vo.MenuVO;
import com.txzt.gaea.vo.PageResultVO;
import com.txzt.gaea.vo.UserDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件服务器对接gaea门户部分用户，菜单接口
 */
@Validated
@RestController
@RequestMapping("/user")
public class UserController {

    @Value("${deptConfig.rootDeptId}")
    Long rootDeptId;

    @Autowired
    private AdminUserRemoteService adminUserRemoteService;


    /**
     * 从门户获取用户信息
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @PostMapping("/detail/query/remote")
    public RtnInfo<UserDetailVO> query(@RequestBody UserTokenDTO dto) throws Exception {
        return adminUserRemoteService.detailQueryRemote(dto);
    }

    /**
     * 从门户获取用户菜单的
     *
     * @param dto
     * @return
     */
    @PostMapping("/menu/query/remote")
    public RtnInfo menuQueryRemote(@RequestBody MenuQueryRemoteDTO dto) {
        RtnInfo<List<MenuSSVO>> menuInfo = adminUserRemoteService.menuQueryRemote(dto);
        List<Map<String,Object>> routerList = new ArrayList<>();
        for(MenuSSVO menuSSVO : menuInfo.getData()){
            Map<String,Object> first = new HashMap<>();
            first.put("path",menuSSVO.getUrl());
            first.put("component","Layout");
            Map<String,Object> meta = new HashMap<>();
            meta.put("title",menuSSVO.getName());
            meta.put("icon",menuSSVO.getIcon());
            first.put("meta",meta);
            if (null != menuSSVO.getChildren()){
                List<Map<String,Object>> childrenRouterList = new ArrayList<>();
                for (MenuVO childrenMenuSSVO : menuSSVO.getChildren()){
                    Map<String,Object> second = new HashMap<>();
                    second.put("path",childrenMenuSSVO.getUrl());
                    String[] cUrl = childrenMenuSSVO.getUrl().split("/");
                    second.put("component","@/views" + menuSSVO.getUrl()+ childrenMenuSSVO.getUrl() + ".vue");
                    second.put("name",menuSSVO.getUrl().split("/")[1]);
                    Map<String,Object> childrenMeta = new HashMap<>();
                    childrenMeta.put("title",childrenMenuSSVO.getName());
                    childrenMeta.put("icon",childrenMenuSSVO.getIcon());
                    second.put("meta",childrenMeta);
                    childrenRouterList.add(second);
                }
                first.put("children",childrenRouterList);
            }else {
                first.put("children",menuSSVO.getChildren());
            }
            routerList.add(first);
        }
//        return adminUserRemoteService.menuQueryRemote(dto);
        return RtnInfo.success(routerList);
    }


    /**
     * 从门户获取用户列表的
     * @param dto
     * @return
     */
    @PostMapping({"/page/query"})
    public RtnInfo<PageResultVO<UserDetailVO>> pageQuery(@RequestBody UserPageDTO dto){
        if (null == dto.getRootDeptId()){
            dto.setRootDeptId(rootDeptId);
        }
        if (1 == dto.getOffset()) {
            dto.setOffset(0);
        }else if(1 < dto.getOffset()){
            dto.setOffset((dto.getOffset() - 1) *  dto.getLimit());
        }
        return adminUserRemoteService.pageQuery(dto);
    }

    @PostMapping({"/all/query"})
    public RtnInfo<PageResultVO<UserDetailVO>> allUsers(@RequestBody UserPageAllDTO userPageAllDTO){
        return adminUserRemoteService.allQuery(userPageAllDTO);
    }
}
